package com.wang.sort;
//有序数组中，找某一个数是否存在
public class Demo6_BSExsit {
    public static boolean exist(int[] sortedArr,int num){
        if(sortedArr == null || sortedArr.length == 0){
            return false;
        }
        int L = 0;
        int R = sortedArr.length - 1;
        int mid = 0;
        while (L <= R){
            //mid = (L + R) / 2 当数组标过大时可能会溢出，不安全
            mid = L + ((R - L) >> 1);
            if(sortedArr[mid] == num){
                return true;
            }
            else if (sortedArr[mid] > num){
                R = mid -1;
            }
            else{
                L = mid -1;
            }
        }
        return sortedArr[L] == num;
    }
}
